﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Citrix.Cmp;

namespace LegacyApp
{
    public partial class Form1 : Form
    {
        private BindingSource source1;

        private CmpApi _cmpApi;

        private ScalableFormControls _formControls;

        /// <summary>
        /// Constructor.
        /// </summary>
        public Form1()
        {
            InitializeComponent();

            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            _cmpApi = new CmpApi();
            _cmpApi.SessionManager.SessionConnected += new EventHandler<SessionConnectedArgs>(SessionManager_SessionConnected);

            // Look for all the controls with Font objects on the form so we can rescale the fonts.
            _formControls = WinformsScalingHelper.MapControls(this);

            // Set the initial font scaling.
            WinformsScalingHelper.DpiScaleFonts(_cmpApi, _formControls);

            PopulateDataViewAndFilter();
        }

        /// <summary>
        /// This event fires whenever a Citrix client reconnects to the server. We use it to refresh the DPI
        /// scaling of fonts to suit the DPI of the client device.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SessionManager_SessionConnected(object sender, SessionConnectedArgs e)
        {
            WinformsScalingHelper.DpiScaleFonts(_cmpApi, _formControls);
        }

        #region Form implementation

        private void Application_ApplicationExit(object sender, EventArgs e)
        {
            _cmpApi.Dispose();
        }

        private void PopulateDataViewAndFilter()
        {
            DataSet set1 = new DataSet();

            // Read the XML database.
            StreamReader reader = File.OpenText(@"Database\PartsDB.xml");
            set1.ReadXml(reader);
            reader.Close();
      
            // Get a DataView of the table contained in the dataset.
            DataTableCollection tables = set1.Tables;
            DataView view1 = new DataView(tables[0]);

            // Create a DataGridView control and add it to the form.
            
            // Create a BindingSource and set its DataSource property to
            // the DataView.
            source1 = new BindingSource();
            source1.DataSource = view1;

            // Set the data source for the DataGridView.
            dataGridView1.DataSource = source1;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            source1.Filter = "((PartNumber LIKE '" + textBox1.Text.ToString() + "*') " + "or (Location LIKE '" + textBox1.Text.ToString() + "*'))";
        }

        private void IDOK_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

        #region Auto keyboard popup integration

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            _cmpApi.Input.BeginShowKeyboard(new KeyboardState(), null);
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            _cmpApi.Input.BeginShowKeyboard(new KeyboardState(), null);
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            _cmpApi.Input.BeginHideKeyboard(null);
        }

        #endregion
    }
}
